package cn.wolfcode.wolf2w.strategy.listener;

import cn.wolfcode.wolf2w.strategy.api.domain.Strategy;
import cn.wolfcode.wolf2w.strategy.service.IStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class RedisDataInitListener implements ApplicationListener<ContextStartedEvent> {


    @Autowired
    private IStrategyService strategyService;

    //当容器启动时候马上执行该方法
    @Override
    public void onApplicationEvent(ContextStartedEvent event) {
        System.out.println("--------------------------redis数据初始化-begin-----------------------------");
        //从mysql 查询所有满足条件攻略数据
        List<Strategy> list = strategyService.list();   //挑选一个热数据（各种统计数相对好看 + 时间）
        for (Strategy strategy : list) {
            //遍历封装成hash， 缓存 到redis中
            strategyService.initStrategyHash(strategy.getId());
        }
        System.out.println("--------------------------redis数据初始化-end-----------------------------");
    }
}
